/*
* Conditions Of Use
*
* This software was developed by employees of the Sigmatrix(Beijing) Corporation.
* This software is provided by sigmatrix as a service and is expressly
* provided "AS IS."  Sigmatrix MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
* OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
* AND DATA ACCURACY.  Sigmatrix does not warrant or make any representations
* regarding the use of the software or the results thereof, including but
* not limited to the correctness, accuracy, reliability or usefulness of
* the software.
*
* Permission to use this software is contingent upon your acceptance
* of the terms of this agreement.
*
*/
package com.sigmatrix.h5.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import com.sigmatrix.h5.dao.DevanningPackingBoxDao;
import com.sigmatrix.h5.dao.impl.common.impl.BaseDaoImpl;
import com.sigmatrix.h5.entity.DevanningPackingBox;
import com.sigmatrix.h5.enums.TableEnum;
import com.sigmatrix.h5.exception.DaoException;
import com.sigmatrix.h5.utils.Cons;

/**
 * @ClassName: DevanningPackingBoxDaoImpl
 * @Description: 拆补箱操作数据库操作接口实现
 * @author youjun 
 * @date 2017-07-08 15:21:46
 */
@Repository(value = "devanningPackingBoxDao")
public class DevanningPackingBoxDaoImpl extends BaseDaoImpl<DevanningPackingBox> implements DevanningPackingBoxDao {

	/** 注入jdbcTemplate */
	@Autowired
	private transient JdbcTemplate jdbcTemplate;

	/**
	 * (non-Javadoc)
	 * 
	 * @Title: findById
	 * @Description: 
	 * @see com.sigmatrix.sc.handheld.inter.dao.DevanningPackingBoxDao#findById(java.lang.Long,
	 *      java.lang.String)
	 * @author youjun
	 * @date 2017年7月15日 下午2:00:51
	 */
	@Override
	public DevanningPackingBox findById(final Long id, final String enterpriseCode) throws DaoException {
		final StringBuilder sql = new StringBuilder("SELECT * FROM ");
		sql.append(getTname(enterpriseCode, TableEnum.DEVANNING_PACKING_BOX)).append(" WHERE _id = ?");
		final Object[] params = new Object[] { id };
		final List<DevanningPackingBox> list = jdbcTemplate.query(sql.toString(), params,
				new DevanningPackingBoxRowMapper());
		if (CollectionUtils.isEmpty(list)) {
			return null;
		}
		return list.get(0);
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @Title: save
	 * @Description: 
	 * @see com.sigmatrix.sc.handheld.inter.dao.DevanningPackingBoxDao#save(com.sigmatrix.sc.handheld.inter.entity.DevanningPackingBox,
	 *      java.lang.String)
	 * @author youjun
	 * @date 2017年7月15日 下午2:01:03
	 */
	@Override
	public int save(final DevanningPackingBox devanningPackingBox, final String enterpriseCode)
			throws DaoException {

		final StringBuilder sql = new StringBuilder("INSERT INTO ")
				.append(getTname(enterpriseCode, TableEnum.DEVANNING_PACKING_BOX))
				.append(" (_work_order, _number, _create_time, _creator_id, _terminal_id, _warehouse_id, _product_id,")
				.append(" _current_wrap_code, _sub_wrap_codes, _opre_type, _status, _remarks) ")
				.append("VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
		final Object[] params = new Object[] { devanningPackingBox.getWorkOrder(), devanningPackingBox.getNumber(),
				devanningPackingBox.getCreateTime(), devanningPackingBox.getCreatorId(),
				devanningPackingBox.getTerminalId(), devanningPackingBox.getWarehouseId(),
				devanningPackingBox.getProductId(), devanningPackingBox.getCurrentWrapCode(),
				devanningPackingBox.getSubWrapCodes(), devanningPackingBox.getOpreType(),
				devanningPackingBox.getStatus(), devanningPackingBox.getRemarks()
				};
		final int[] argTypes = new int[] { Types.VARCHAR, Types.INTEGER, Types.TIMESTAMP, Types.INTEGER, Types.INTEGER,
				Types.INTEGER, Types.INTEGER, Types.VARCHAR, Types.LONGVARCHAR, Types.SMALLINT, Types.SMALLINT,
				Types.VARCHAR
				};
		return jdbcTemplate.update(sql.toString(), params, argTypes);
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @Title: update
	 * @Description: 
	 * @see com.sigmatrix.sc.handheld.inter.dao.DevanningPackingBoxDao#update(com.sigmatrix.sc.handheld.inter.entity.DevanningPackingBox,
	 *      java.lang.String)
	 * @author youjun
	 * @date 2017年7月15日 下午2:01:21
	 */
	@Override
	public int update(final DevanningPackingBox devanningPackingBox, final String enterpriseCode)
			throws DaoException {

		final StringBuilder sql = new StringBuilder("UPDATE ").append(getTname(enterpriseCode, TableEnum.DEVANNING_PACKING_BOX));
		sql.append(" SET _work_order = ?, _number = ?, _create_time = ?, _creator_id = ?, _terminal_id = ?, _warehouse_id = ?, _product_id = ?, _current_wrap_code = ?, _sub_wrap_codes = ?, _opre_type = ?, _status = ?, _remarks = ? WHERE _id = ?");
		final Object[] params = new Object[] { devanningPackingBox.getWorkOrder(), devanningPackingBox.getNumber(),
				devanningPackingBox.getCreateTime(), devanningPackingBox.getCreatorId(),
				devanningPackingBox.getTerminalId(), devanningPackingBox.getWarehouseId(),
				devanningPackingBox.getProductId(), devanningPackingBox.getCurrentWrapCode(),
				devanningPackingBox.getSubWrapCodes(), devanningPackingBox.getOpreType(),
				devanningPackingBox.getStatus(), devanningPackingBox.getRemarks(), devanningPackingBox.getId() };
		final int[] argTypes = new int[] { Types.VARCHAR, Types.INTEGER, Types.TIMESTAMP, Types.INTEGER, Types.INTEGER,
				Types.INTEGER, Types.INTEGER, Types.VARCHAR, Types.LONGVARCHAR, Types.SMALLINT, Types.SMALLINT,
				Types.VARCHAR, Types.BIGINT };
		return jdbcTemplate.update(sql.toString(), params, argTypes);
	}

	/**
	 * (non-Javadoc)
	 *@Title: findListByCondition
	 *@Description: 
	 *@see com.sigmatrix.sc.handheld.inter.dao.DevanningPackingBoxDao#findListByCondition(com.sigmatrix.sc.handheld.inter.entity.DevanningPackingBox, java.lang.String)
	 *@author youjun
	 *@date 2017年7月15日 下午2:02:16
	 */
	@Override
	public List<DevanningPackingBox> findListByCondition(final DevanningPackingBox devanningPackingBox, final String enterpriseCode)
			throws DaoException {

		final StringBuilder sql = new StringBuilder("SELECT * FROM ").append(getTname(enterpriseCode, TableEnum.DEVANNING_PACKING_BOX));
		final List<Object> paramList = new ArrayList<Object>();
		sql.append(" WHERE _status <> ?");
		paramList.add(Cons.STATUS_DELETE);
		try {
			if(devanningPackingBox!=null) {
				if(StringUtils.isNotBlank(devanningPackingBox.getWorkOrder())) {
					sql.append(" AND _work_order = ?");
					paramList.add(devanningPackingBox.getWorkOrder());
				}
			}
			return jdbcTemplate.query(sql.toString(), paramList.toArray(), new DevanningPackingBoxRowMapper());
		} catch (DataAccessException e) {
			throw new DaoException(e);
		}
	}

	/**
	 * @ClassName: DevanningPackingBoxRowMapper
	 * @Description: 封装返回的实体
	 * @author youjun @date 2017-07-08 15:21:46
	 */
	public class DevanningPackingBoxRowMapper implements RowMapper<DevanningPackingBox> {

		/**
		 * (non-Javadoc)
		 * @Title: mapRow
		 * @Description: 
		 * @see
		 * @author youjun 
		 * @date 2017-07-08 15:21:46
		 */
		@Override
		public DevanningPackingBox mapRow(final ResultSet resultSet, final int rowNum) throws SQLException {
			final DevanningPackingBox devanningPackingBox = new DevanningPackingBox();
			devanningPackingBox.setId(resultSet.getLong("_id"));
			devanningPackingBox.setWorkOrder(resultSet.getString("_work_order"));
			devanningPackingBox.setNumber(resultSet.getInt("_number"));
			devanningPackingBox.setCreateTime(resultSet.getTimestamp("_create_time"));
			devanningPackingBox.setCreatorId(resultSet.getInt("_creator_id"));
			devanningPackingBox.setTerminalId(resultSet.getInt("_terminal_id"));
			devanningPackingBox.setWarehouseId(resultSet.getInt("_warehouse_id"));
			devanningPackingBox.setProductId(resultSet.getInt("_product_id"));
			devanningPackingBox.setCurrentWrapCode(resultSet.getString("_current_wrap_code"));
			devanningPackingBox.setSubWrapCodes(resultSet.getString("_sub_wrap_codes"));
			devanningPackingBox.setOpreType(resultSet.getShort("_opre_type"));
			devanningPackingBox.setStatus(resultSet.getShort("_status"));
			devanningPackingBox.setRemarks(resultSet.getString("_remarks"));
			return devanningPackingBox;
		}
	}

}
